import cheerio from 'cheerio'

export const getTags = async (client,url)=>{
    let contents,$,id,name,tags=[]
    contents = await client.customRequest(url, {
        method: "PROPFIND",
        headers: {
            Accept: "text/xml",
            Depth: "infinity",
            "Content-Type": "application/xml; charset=utf-8"
        },
        data: `<?xml version="1.0"?>
                    <d:propfind  xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">
                      <d:prop>
                        <oc:id />
                        <oc:display-name />
                        <oc:user-visible />
                        <oc:user-assignable />
                        <oc:can-assign />
                      </d:prop>
                    </d:propfind>`,
        responseType: "text"
    });
    $ = cheerio.load(contents.data, {
        xmlMode: true
    });
    $("d\\:multistatus d\\:response d\\:propstat d\\:prop").each((index,e)=>{
        id = $(e).find("oc\\:id").text()
        name = $(e).find("oc\\:display-name").text()
        if(id&&name){
            tags.push({id,name})
        }
    })
    return tags;
}

export const addTags = async (client,tags)=>{
    for(let tag of tags){
        try{
            await client.customRequest("/systemtags", {
                method: "POST",
                headers: {
                    "Content-Type": "application/json; charset=utf-8"
                },
                data: {
                    "userVisible": true,
                    "userAssignable": true,
                    "canAssign": true,
                    "name": tag
                },
                responseType: "json"
            });
        }catch(err){
            console.log(err)
        }
    }
}

export const getFileTag = async (client,fileid)=>{
    let tags = await getTags(client,"/systemtags-relations/files/" + fileid)
    return tags
}

export const tagFile = async (client,fileid,tags)=>{
    for(let tagid of tags){
        try{
            await client.customRequest(`/systemtags-relations/files/${fileid}/${tagid}`, {
                method: "PUT",
                headers: {
                    "Content-Type": "application/json; charset=utf-8"
                },
                responseType: "json"
            });
        }catch(err){
            console.log(err)
            if(err.response.status==404){
                throw err
            }
        }
    }
}

export const untagFile = async (client,fileid,tags)=>{
    for(let tagid of tags){
        try{
            await client.customRequest(`/systemtags-relations/files/${fileid}/${tagid}`, {
                method: "DELETE",
                headers: {
                    "Content-Type": "application/json; charset=utf-8"
                },
                responseType: "json"
            });
        }catch(err){
            console.log(err)
        }
    }
}

export const delTag = async (client,id)=>{
    await client.customRequest("/systemtags/" + id, {
        method: "DELETE",
        headers: {
            "Content-Type": "application/json; charset=utf-8"
        },
        responseType: "json"
    });
}